package com.yanqu.road.dao.impl.activity.hell;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hell.HellNpc;
import com.yanqu.road.entity.activity.hell.HellUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class HellUserDataDaoImpl extends TempDataObjectDao<HellUserData> {


    public static String getHellUserDataSql = "select * from t_u_activity_hell where activityId=? and userId=?";
    public HellUserData getHellUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getHellUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
                call.setLong(i++,userId);
                rs = call.executeQuery();
                while (rs.next()) {
                    HellUserData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertHellUserDataSql = "insert into t_u_activity_hell(activityId,userId,integral,npcCount,npcData,updateTime) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(HellUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNpcCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getNpcMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertHellUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateHellUserDataSql = "update t_u_activity_hell set integral=?,npcCount=?,updateTime=?,npcData=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(HellUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNpcCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getNpcMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateHellUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected HellUserData parseData(ResultSet rs) throws SQLException {
        HellUserData data = new HellUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setIntegral(rs.getLong("integral"));
        data.setNpcCount(rs.getInt("npcCount"));
        data.setUpdateTime(rs.getLong("updateTime"));
        String str  = rs.getString("npcData");
        if(!StringUtils.isNullOrEmpty(str)){
            Map<Integer, Map<Integer, HellNpc>> result = JSON.parseObject(str, new TypeReference<Map<Integer, Map<Integer,HellNpc>>>() {});
            if(result != null){
                data.setNpcMap(result);
            }
        }
        return data;
    }

}
